#!/usr/bin/env python
# yongming.li 

import sys
import os
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *


bootchartPath = os.path.dirname(__file__)
print 'bootchartPath is : ' + bootchartPath
sys.path.append('../'+bootchartPath)

#define LOG_ROOT        "/data/bootchart"

#define LOG_STAT        LOG_ROOT"/proc_stat.log"
#define LOG_PROCS       LOG_ROOT"/proc_ps.log"
#define LOG_DISK        LOG_ROOT"/proc_diskstats.log"
#define LOG_HEADER      LOG_ROOT"/header"
#define LOG_ACCT        LOG_ROOT"/kernel_pacct"

#define LOG_STARTFILE   "/data/bootchart-start"
#define LOG_STOPFILE    "/data/bootchart-stop"
class BootchartForm(QDialog):



    def  generatebootchart(self):
         # you can modified 120 to another for how many seconds you wanna monitor
         cmds = [
                 #('adb shell "echo 120 > /data/bootchart-start" '),
                 ('adb  pull   /data/bootchart  ./'),
                 ('tar -czf bootchart.tgz header proc_stat.log proc_ps.log  proc_diskstats.log kernel_pacct'),
                 ('tar -czf bootchart.tgz header proc_stat.log proc_ps.log  proc_diskstats.log kernel_pacct'),
                 ('java -jar bootchart.jar -f png  ./')
                ]
         for cmd in cmds:
             os.system(cmd)
             print cmd

         self.bootchartLabel.setPixmap(QPixmap.fromImage(QImage(bootchartPath+'/bootchart.png')).scaled(700,500))
         
    def  showbootchart(self):
        self.showbootchartsbox = QtGui.QGroupBox("will show boot chart here :")
        layout = QtGui.QVBoxLayout()

        self.bootchartLabel = QtGui.QLabel('')
        #self.bootchartLabel.setMinimumSize(300,400)
 
        

        self.generatebutton = QPushButton()
        self.generatebutton.setText ('generate bootchart')
        self.connect(self.generatebutton, SIGNAL('clicked()'), self.generatebootchart)

        layout.addWidget(self.bootchartLabel)
        layout.addWidget(self.generatebutton)

        self.showbootchartsbox.show()
        self.showbootchartsbox.setLayout(layout)

    def  showsteps(self):
        self.showstepsbox = QtGui.QGroupBox("generate bootchart steps :")
        layout = QtGui.QVBoxLayout()
        steps = """
0  bootchart.h
   #ifndef BOOTCHART
   # define  BOOTCHART  1
   #endif
1  echo 120 > /data/bootchart-start
2 sudo apt-get install  ant
3 download  bootchart-0.9.tar.bz2
4 adb shell pull   /data/bootchart   ./data
5 tar -czf bootchart.tgz header proc_stat.log 
  proc_ps.log  proc_diskstats.log kernel_pacct
6 java -jar bootchart.jar -f png  ./
7 more details ref:
  system/core/init/bootchart.c
8 good luck !
========================================================
========================================================
========================================================
""" 
        self.stepsLabel = QtGui.QLabel(steps)
        
        self.secondstomonitorLabel = QtGui.QLabel('seconds to monitor (max : 10*60s)')
        self.secondstomonitorSpinBox = QSpinBox()
        # define BOOTCHART_MAX_TIME_SEC        (10*60) /* max polling time in seconds */


        self.secondstomonitorSpinBox.setRange(30, 10*60)
        self.secondstomonitorSpinBox.setSingleStep(1)

        self.secondstomonitorSpinBox.setValue(120)
        self.secondstomonitorSpinBox.valueChanged.connect(self.setmonitorseconds)
   
        layout.addWidget(self.stepsLabel)
        layout.addWidget(self.secondstomonitorLabel)
        layout.addWidget(self.secondstomonitorSpinBox)
        self.showstepsbox.show()
        self.showstepsbox.setLayout(layout)

    def setmonitorseconds(self,value):
        self.monitorseconds = value
        cmd = 'adb shell "echo %d > /data/bootchart-start" '%(value)
        print cmd
        os.system(cmd)

    def __init__(self, parent=None):
        print 'Bootchart Form'
        super(BootchartForm, self).__init__(parent)
        self.frameStyle = QtGui.QFrame.Sunken | QtGui.QFrame.Panel
        self.monitorseconds = 120

        self.showsteps()

        self.showbootchart()
        mainLayout = QGridLayout()
        
        self.setWindowTitle("Bootchart")

        mainLayout.addWidget(self.showstepsbox,0,0)
        mainLayout.addWidget(self.showbootchartsbox,0,1)

        self.setLayout(mainLayout)
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = None
    form = BootchartForm()
    sys.exit(app.exec_())




